package boot

import (
	"context"

	appConfig "system-admin/config"

	wechat "github.com/silenceper/wechat/v2"
	"github.com/silenceper/wechat/v2/cache"
	"github.com/silenceper/wechat/v2/credential"
	"github.com/silenceper/wechat/v2/officialaccount"
	offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
)

var OfficialAccount *officialaccount.OfficialAccount

func InitWechantAccount() {
	wc := wechat.NewWechat()
	// 设置全局cache，也可以单独为每个操作实例设置
	redisOpts := &cache.RedisOpts{
		Host:     appConfig.Config.Redis.Host + ":" + appConfig.Config.Redis.Port,
		Password: appConfig.Config.Redis.Password,
		Database: appConfig.Config.Redis.Database,
	}
	redisCache := cache.NewRedis(context.Background(), redisOpts)
	wc.SetCache(redisCache)
	cfg := &offConfig.Config{
		AppID:     appConfig.Config.Wechat.AppID,
		AppSecret: appConfig.Config.Wechat.AppSecret,
		Token:     "",
	}
	OfficialAccount = wc.GetOfficialAccount(cfg)
	OfficialAccount.SetAccessTokenHandle(
		credential.NewStableAccessToken(
			appConfig.Config.Wechat.AppID,
			appConfig.Config.Wechat.AppSecret,
			"gowechat_officialaccount__access_token_"+appConfig.Config.Wechat.AppID,
			redisCache,
		),
	)
}
